package com.sqx.modules.fixedClassify.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.fixedClassify.dao.FixedClassifyDao;
import com.sqx.modules.fixedClassify.entity.FixedClassify;
import com.sqx.modules.fixedClassify.service.FixedClassifyService;
import org.apache.poi.ss.formula.functions.Fixed;
import org.jaudiotagger.fix.Fix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FixedClassifyServiceImpl extends ServiceImpl<FixedClassifyDao, FixedClassify> implements FixedClassifyService {
    @Autowired
    private FixedClassifyDao classifyDao;

    @Override
    public Result addClassify(FixedClassify fixedClassify) {
        if (fixedClassify.getParentId() == null) {
            fixedClassify.setParentId(0L);
        } else {
            FixedClassify parentClassify = classifyDao.selectById(fixedClassify.getParentId());
            if (parentClassify == null) {
                return Result.error("选中的一级目录不存在");
            }
        }
        return Result.upStatus(classifyDao.insert(fixedClassify));

    }

    @Override
    public List<FixedClassify> getTreeClassifyList(FixedClassify fixedClassify) {
        List<FixedClassify> entities = classifyDao.selectFixedClassifyListByUserId(fixedClassify.getUserId());
        return entities.stream().filter((item) -> item.getParentId() == 0).peek((menu) -> menu.setChildren(getChildren(menu, entities))).collect(Collectors.toList());
    }

    @Override
    public Result deleteClassify(Long classifyId) {
        FixedClassify fixedClassify = classifyDao.selectById(classifyId);
        if(fixedClassify.getParentId()==0){
            Integer count = classifyDao.selectCount(new QueryWrapper<FixedClassify>().eq("parent_id", fixedClassify.getClassifyId()));
            if(count>0){
                return Result.error("当前目录下还有子菜单");
            }
        }
        return Result.upStatus(classifyDao.deleteById(classifyId));
    }

    @Override
    public Result updateClassify(FixedClassify fixedClassify) {
            FixedClassify classify = new FixedClassify();
            classify.setIsEnable(fixedClassify.getIsEnable());
            classifyDao.update(classify,new QueryWrapper<FixedClassify>().eq("parent_id", fixedClassify.getClassifyId()));
        return Result.upStatus(classifyDao.updateById(fixedClassify));

    }

    @Override
    public List<FixedClassify> getClassifyList(FixedClassify fixedClassify) {
        QueryWrapper<FixedClassify> wrapper = new QueryWrapper<>(fixedClassify);
        if(fixedClassify.getIsEnable()!=null){
            wrapper.eq("is_enable", fixedClassify.getIsEnable());
        }
        return classifyDao.selectList(wrapper.orderByDesc("sort"));

    }

    @Override
    public List<FixedClassify> getClassifyListByUserId(Long userId) {
        List<FixedClassify> fixedClassifies = classifyDao.selectFixedClassifyList(userId);
        /*List<FixedClassify> fixedClassifies1 = classifyDao.selectList(new QueryWrapper<FixedClassify>().eq("is_enable", 1).eq("parent_id", 0));
        for (FixedClassify fixedClassify :fixedClassifies1){
            List<FixedClassify> children=new ArrayList<>();
            for (FixedClassify fixedClassify1:fixedClassifies){
                if(fixedClassify.getClassifyId().equals(fixedClassify1.getParentId())){
                    children.add(fixedClassify1);
                }
            }
            fixedClassify.setChildren(children);
        }*/
        return fixedClassifies;
    }

    /**
     * 递归多级菜单
     *
     * @param root
     * @param all
     * @return
     */
    private List<FixedClassify> getChildren(FixedClassify root, List<FixedClassify> all) {
        return all.stream().filter(categoryEntity -> categoryEntity.getParentId().equals(root.getClassifyId())).peek(categoryEntity -> {
            categoryEntity.setChildren(getChildren(categoryEntity, all));
        }).collect(Collectors.toList());
    }


}
